//公告消息、  转账、充值消息相关服务
package service

import (
	"errors"
	"strconv"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/drive/redis"
	. "zimuzu_cvn_web_api/pkg/e"
)

//获取用户未读消息条数
func GetUserMessageReadNumCacheKey(uid int64) string {
	return UserRedisPrefix + strconv.FormatInt(uid, 10) + UserMessageReadNumCacheKey
}

//GetAnnouncement 获取web  的公告列表
//Auth: Xu
func GetAnnouncement(page int, per int) (announcementList []model.Announcement, err error) {
	if page == 0 {
		page = 1
	}
	if per == 0 {
		per = 3
	}
	announcementList, err = model.GetWebAnnouncement(config.C.DB.DB, page, per)

	return
}

//GetAppAnnouncementNum 获取App 的系统公告
//Auth: Xu
func GetAppAnnouncement(uid int64, page int, per int) (announcementList []model.Announcement, err error) {
	if page == 0 {
		page = 1
	}
	if per == 0 {
		per = 3
	}
	announcementList, err = model.GetAppAnnouncement(config.C.DB.DB, page, per)
	if err != nil {
		return announcementList, errors.New("获取系统消息列表失败")
	}
	UpdateNoticeMessageReadCache(uid)

	return
}

//GetAppAnnouncementNum 获取App 的系统公告数量
//Auth: Xu
func GetAppAnnouncementNum() (num int, err error) {

	num, err = model.GetAppAnnouncementNum(config.C.DB.DB)
	if err != nil {
		return 0, errors.New("查询系统消息数失败")
	}
	return
}

//GetAccountLogNums 查询转账消息条数
//Auth: Xu
func GetAccountLogNums(uid int64) (cvntNum, otherNum int) {
	cvntNum, _ = model.GetAccountLogNums(config.C.DB.DB, uid)
	otherNum, _ = model.GetUsdtLogNums(config.C.DB.DB, uid)

	return
}

//查询是否有未读消息
//Auth: Xu
func GetUnreadMessageNum(uid int64) (dealUnreadNum, otherUnreadMsgNum, noticeUnreadNum int, err error) {
	var dealReadNums, otherDealReadNum, noticeReadNums int
	readNumsCacheKey := GetUserMessageReadNumCacheKey(uid)
	if redis.Exists(readNumsCacheKey) {
		cacheReadNums, _ := redis.HGetAll(readNumsCacheKey)
		if deal, ok := cacheReadNums["deal"]; ok {
			dealReadNums, _ = strconv.Atoi(deal.(string))
		}
		if otherDeal, ok := cacheReadNums["otherDeal"]; ok {
			otherDealReadNum, _ = strconv.Atoi(otherDeal.(string))
		}
		if notice, ok := cacheReadNums["notice"]; ok {
			noticeReadNums, _ = strconv.Atoi(notice.(string))
		}
	}
	dealTotalNums, dealOtherTotalNums := GetAccountLogNums(uid)

	noticeTotalNums, err := GetAppAnnouncementNum()
	if err != nil {
		return
	}
	dealUnreadNum = dealTotalNums - dealReadNums
	otherUnreadMsgNum = dealOtherTotalNums - otherDealReadNum
	noticeUnreadNum = noticeTotalNums - noticeReadNums
	return
}

//更新转账已读消息条数缓存
//Auth: Xu
func UpdateDealMessageReadCache(uid int64) error {
	dealTotalNums, otherDealTotalNums := GetAccountLogNums(uid)
	readNumsCacheKey := GetUserMessageReadNumCacheKey(uid)
	data := map[string]interface{}{
		"deal":      dealTotalNums,
		"otherDeal": otherDealTotalNums,
	}
	redis.HSet(readNumsCacheKey, data, -1)
	return nil
}

//更新系统已读消息条数缓存
//Auth: Xu
func UpdateNoticeMessageReadCache(uid int64) error {
	noticeTotalNums, err := GetAppAnnouncementNum()
	if err != nil {
		return nil
	}
	readNumsCacheKey := GetUserMessageReadNumCacheKey(uid)
	data := map[string]interface{}{
		"notice": noticeTotalNums,
	}
	redis.HSet(readNumsCacheKey, data, -1)

	return nil
}

//获取转账、充值消息列表
//Auth: Xu
func GetDealMessageList(uid int64, per, page, Type int) (dealMessages []model.DealMessage, err error) {
	var currency string
	if Type == 0 {
		currency = "CVNT"
		cvnList, err := model.GetDeaMessageList(uid, per, page, currency)
		if err != nil {
			return dealMessages, errors.New("获取转账消息列表失败")
		}
		dealMessages = append(dealMessages, cvnList...)
	} else {
		currency = "USDT"
		usdtList, err := model.GetDeaUsdtMessageList(uid, per, page, currency)
		if err != nil {
			return dealMessages, errors.New("获取usdt转账消息列表失败")
		}
		dealMessages = append(dealMessages, usdtList...)
	}

	for k, v := range dealMessages {
		if v.Source == model.LogSourceOut || v.Source == model.LogSourceUsdtOut || v.Source == model.LogSourceInnerOut || v.Source == model.LogSourceUsdtInnerOut {
			if v.Status == model.DealStatusSuccess || v.Source == model.LogSourceInnerOut || v.Source == model.LogSourceUsdtInnerOut {
				dealMessages[k].Des = "转账成功：" + currency
			} else if v.Status == model.DealStatusFail {
				dealMessages[k].Des = "转账失败：" + currency
			} else {
				dealMessages[k].Des = "转账确认中：" + currency
			}
		} else {
			dealMessages[k].Des = "已收到：" + currency
		}
	}

	UpdateDealMessageReadCache(uid)

	return
}

//查询系统消息详情
//Auth: Xu
func GetNoticeMessageNotice(id int) (model.Announcement, error) {
	messageDetail, err := model.GetAppAnnouncementDetail(config.C.DB.DB, id)
	if err != nil {
		return messageDetail, errors.New("查询系统消息详情失败")
	}
	return messageDetail, err
}
